package com.dst.smart.json;

import java.io.File;
import org.json.JSONObject;

import com.dst.smart.app.MyApplication;
import com.dst.smart.model.RootPath;
import android.text.TextUtils;

public class ProjectUtils {

	public static RootPath rootPath = null;

	private static File jsonFile = new File(MyApplication.getInstance()
			.getRootPath() + File.separator + "root_json");

	public static RootPath getRootPath() {
		if (rootPath == null) {
			rootPath = new RootPath();
			String json = JSONFileUtils.getJsonStringFromFile(jsonFile)
					.replace("\r\n", "");
			if (TextUtils.isEmpty(json)) {
				rootPath.setRootPath(MyApplication.getInstance().getRootPath()
						+ File.separator + "contactsmanager.db");
				rootPath.setRootName("Root");
				rootPath.setRootWay(0);
				rootPath.setRootID("");
				rootPath.setConnectState(1);
				rootPath.setMessageBack(0);
				rootPath.setLanguage(0);
				rootPath.setRootTime("00");
				rootPath.setIsSpeech(0);
				rootPath.setIsLoop(0);
				rootPath.setIsFeedBack(1);
				rootPath.setInteractive(0);
				rootPath.setServiceIP("103.40.22.138");
				rootPath.setCity("");
				rootPath.setIsVoiceAutoAdj(0);
				rootPath.setAirQuality("0,0,0,0,0,0,0");
				rootPath.setIllumination("0,0,0,0,0,0,0");
				saveRootPath();
			} else {
				try {
					JSONObject object = new JSONObject(json);
					if(object.has("RootPath")) {
						rootPath.setRootPath(object.getString("RootPath"));
					}else{
						rootPath.setRootPath(MyApplication.getInstance().getRootPath()
								+ File.separator + "contactsmanager.db");
					}
					if(object.has("RootName")) {
						rootPath.setRootName(object.getString("RootName"));
					}
					else{
						rootPath.setRootName("Root");
					}
					if(object.has("rootWay")) {
						rootPath.setRootWay(object.getInt("rootWay"));
					}else{
						rootPath.setRootWay(0);
					}

					if(object.has("rootID")) {
						rootPath.setRootID(object.getString("rootID"));
					}else{
						rootPath.setRootID("");
					}
					if(object.has("connectState")) {
						rootPath.setConnectState(object.getInt("connectState"));
					}else{
						rootPath.setConnectState(1);
					}
					if(object.has("messageBack")) {
						rootPath.setMessageBack(object.getInt("messageBack"));
					}else{
						rootPath.setMessageBack(0);
					}
					if(object.has("language")) {
						rootPath.setLanguage(object.getInt("language"));
					}else{
						rootPath.setLanguage(0);
					}
					if(object.has("rootTime")) {
						rootPath.setRootTime(object.getString("rootTime"));
					}else{
						rootPath.setRootTime("00");
					}
					if(object.has("isSpeech")) {
						rootPath.setIsSpeech(object.getInt("isSpeech"));
					}else{
						rootPath.setIsSpeech(0);
					}
					if(object.has("isLoop")) {
						rootPath.setIsLoop(object.getInt("isLoop"));
					}else{
						rootPath.setIsLoop(0);
					}
					if(object.has("isFeedBack")) {
						rootPath.setIsFeedBack(object.getInt("isFeedBack"));
					}else{
						rootPath.setIsFeedBack(1);
					}
					if(object.has("Interactive")) {
						rootPath.setInteractive(object.getInt("Interactive"));
					}else{
						rootPath.setInteractive(0);
					}
					if(object.has("ServiceIP")) {
						rootPath.setServiceIP(object.getString("ServiceIP"));
					}else{
						rootPath.setServiceIP("103.40.22.138");
					}
					if(object.has("City")) {
						rootPath.setCity(object.getString("City"));
					}else{
						rootPath.setCity("");
					}
					if(object.has("isVoiceAutoAdj")) {
						rootPath.setIsVoiceAutoAdj(object.getInt("isVoiceAutoAdj"));
					}else{
						rootPath.setIsVoiceAutoAdj(0);
					}
					if(object.has("AirQuality")) {
						rootPath.setAirQuality(object.getString("AirQuality"));
					}else{
						rootPath.setAirQuality("0,0,0,0,0,0,0");
					}
					if(object.has("Illumination")) {
						rootPath.setIllumination(object.getString("Illumination"));
					}else{
						rootPath.setIllumination("0,0,0,0,0,0,0");
					}
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
		return rootPath;
	}

	public static void saveRootPath() {
		try {
			JSONObject bject = new JSONObject();
			bject.put("RootPath", rootPath.getRootPath());
			bject.put("RootName", rootPath.getRootName());
			bject.put("rootWay", rootPath.getRootWay());
			bject.put("rootID", rootPath.getRootID());
			bject.put("connectState", rootPath.getConnectState());
			bject.put("messageBack", rootPath.getMessageBack());
			bject.put("language", rootPath.getLanguage());
			bject.put("rootTime", rootPath.getRootTime());
			bject.put("isSpeech", rootPath.getIsSpeech());
			bject.put("isLoop", rootPath.getIsLoop());
			bject.put("isFeedBack", rootPath.getIsFeedBack());
			bject.put("Interactive", rootPath.getInteractive());
			bject.put("ServiceIP", rootPath.getServiceIP());
			bject.put("City", rootPath.getCity());
			bject.put("isVoiceAutoAdj", rootPath.getIsVoiceAutoAdj());
			bject.put("AirQuality", rootPath.getAirQuality());
			bject.put("Illumination", rootPath.getIllumination());
			JSONFileUtils.saveJSONToFile(jsonFile, bject.toString());
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}

}
